﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using UCS.XNA.Utils.Animation;

namespace UCS.XNA.Utils
{
    public class Background : IFadeAnimatable
    {
        #region Private Members

        private Rectangle _bounds;
        private Texture2D _texture;
        private FadeAnimation _fadeAnimation;
        private bool _visible;

        #endregion

        #region Attributes and Properties

        private float _transparency = 1;
        public float Transparency
        {
            get { return this._transparency; }
            set { this._transparency = value; }
        }

        #endregion

        #region Events

        public event EventHandler<EventArgs> Hid;

        #endregion

        #region Constructors

        public Background(Rectangle bounds, Texture2D texture)
        {
            this._bounds = bounds;
            this._texture = texture;
            this._visible = true;
        }

        #endregion

        #region Private Methods

        private void RaiseHidEvent()
        {
            if (this.Hid != null)
                this.Hid(this, new EventArgs());
        }

        #endregion

        #region Public Methods

        public void Update(GameTime gameTime)
        {
            if (this._fadeAnimation != null)
                this._fadeAnimation.Update(gameTime);
        }

        public void Draw(GameTime gameTime)
        {
            if (this._visible)
            {
                GameResources.SpriteBatch.Begin();

                GameResources.SpriteBatch.Draw(this._texture, this._bounds, Color.White * this._transparency);

                GameResources.SpriteBatch.End();
            }
        }

        public void Show()
        {
            this._visible = true;

            this._fadeAnimation = new FadeInAnimation(this, 0.05f);
            this._fadeAnimation.AnimationFinished += (sender, e) => this._fadeAnimation = null; ;
        }

        public void Hide()
        {
            this._fadeAnimation = new FadeOutAnimation(this, 0.05f);
            this._fadeAnimation.AnimationFinished += (sender, e) => { this._visible = false; this._fadeAnimation = null; this.RaiseHidEvent(); };
        }

        #endregion
    }
}